package zx.x.modules.app.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import zx.x.modules.app.entity.OccupantEntity;
import zx.x.modules.app.service.OccupantService;
import zx.x.common.utils.PageUtils;
import zx.x.common.utils.R;



/**
 * 入驻人信息维护表
 *
 * @author x
 * @email x
 * @date 2022-10-31 16:41:17
 */
@RestController
@RequestMapping("/app/occupant")
public class OccupantController {
    @Autowired
    private OccupantService occupantService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(OccupantEntity occupant){
        QueryWrapper<OccupantEntity> queryWrapper = new QueryWrapper<OccupantEntity>();
        queryWrapper.eq("user_id",occupant.getUserId());
        Page page = PageHelper.startPage(occupant.getPage(),occupant.getLimit());
        return R.ok().put("page", occupantService.list(queryWrapper)).put("pages",page.getPages()).put("total",page.getTotal());
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		OccupantEntity occupant = occupantService.getById(id);
        return R.ok().put("occupant", occupant);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(OccupantEntity occupant){
		occupantService.save(occupant);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(OccupantEntity occupant){
        if(occupant.getDeleteFlag() != null && occupant.getDeleteFlag() == 1){
            occupantService.removeById(occupant);
            return R.ok();
        }
		occupantService.updateById(occupant);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(Integer[] ids){
		occupantService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
